package org.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author huangshen
 * @version 0.0.3
 * @description TypeUtils
 * @since 2022/1/28 11:09
 */
public class TypeUtils {

    private TypeUtils() {
    }

    public static <E extends Annotation> Map<String, Class<?>> getParamTypesByAnnotation(Class<?> targetClass, Class<E> annotationClass) {
        Map<String, Class<?>> map = new HashMap<>();
        for (Field field : targetClass.getDeclaredFields()) {
            if (!Function.equalsNull(field.getAnnotation(annotationClass))) {
                map.put(field.getName(), field.getType());
            }
        }
        return map;
    }

    public static Map<String, Class<?>> getParamTypes(Class<?> targetClass) {
        Integer size = MathUtils.getMapLength(targetClass.getDeclaredFields().length);
        Map<String, Class<?>> map = new HashMap<>(size);
        for (Field field : targetClass.getDeclaredFields()) {
            map.put(field.getName(), field.getType());
        }
        return map;
    }
}
